package com.gzhryc.shared_device.oem.h5.agent.outside;

import com.gzhryc.common.api.DefaultAPICodeConstants;
import com.gzhryc.common.json.JsonTools;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.common.secruity.EncryptTools;
import com.gzhryc.shared_device.common.payment.dao.models.WXTokenHttpConfig;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.net.URI;
import java.nio.charset.Charset;

/**
 * 获取外部公众号Token接口
 */
public class GZHHttpAPIService  {

    private static Logger log = Logger.getLogger(GZHHttpAPIService.class);

    private WXTokenHttpConfig httpConfig;
    private CloseableHttpClient httpClient;

    public GZHHttpAPIService(WXTokenHttpConfig httpConfig) {
        this.httpConfig = httpConfig;
        this.httpClient = HttpClients.createDefault();
    }

    public GetGZHTokenResponse getAccessToken(String appId) {
        CloseableHttpResponse response = null;
        try {
            URI uri = new URIBuilder(httpConfig.getUrl()).build();

            GetGZHTokenRequest apiReq = new GetGZHTokenRequest();
            apiReq.getBody().setAppId(appId);

            StringEntity strEntity = new StringEntity(JsonTools.toJson(apiReq), ContentType.create("application/json", Consts.UTF_8));

            Long timestamp = (System.currentTimeMillis() / 1000) / 10;
            String token = EncryptTools.md5(httpConfig.getToken() + timestamp);

            HttpPost httpPost = new HttpPost(uri);
            httpPost.setHeader("authorization",token);
            httpPost.setEntity(strEntity);

            response = httpClient.execute(httpPost);
            HttpEntity entity = response.getEntity();
            if(entity != null){
                String str = EntityUtils.toString(entity, Charset.forName("UTF-8"));
                log.info("获取微信公众号Token返回：" + str);
                if(StringUtils.isNotBlank(str)) {
                    GetGZHTokenResponse apiResp = JsonTools.fromJson(str, GetGZHTokenResponse.class);
                    if(apiResp != null) {
                        if(DefaultAPICodeConstants.SUCCESS.equals(apiResp.getCode())) {
                           return apiResp;
                        }
                    }
                }
            }
        } catch(Exception e){
            log.error("获取微信公众号Token错误，" + e.getMessage(), e);
        } finally {
            if(response != null){
                try {
                    response.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }
}
